﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using My.Data.Factories;
using System.Windows.Forms;
using My.Data.BusinessObject;
using My.Data.DataAccess;

namespace Gui
{
    # region The Implementation

    public interface IBridge
    {
        void deleteUser(DataGridView mainTable);
        void fillTable(DataGridView mainTable);
        void clearTable(DataGridView mainTable);
        void signInCourse(int userId_);
    }

    public class AdminTeacherBridge : IBridge
    {
        #region IBridge Members
        public void deleteUser(DataGridView mainTable)
        {
            ITransaction transaction = null;
            ISession session = ISessionManager.Instance.CreateISession();
            transaction = session.BeginTransaction();
            Int32 selectedRowCount =
            mainTable.Rows.GetRowCount(DataGridViewElementStates.Selected);
            Person person = new Person();
            if (selectedRowCount > 0)
            {

                for (int i = 0; i < selectedRowCount; i++)
                {
                    int index = mainTable.SelectedRows[i].Index;

                    person = new Person();
                    person.Id = int.Parse(mainTable.Rows[index].Cells[0].Value.ToString());
                    session.Delete(person);
                }
            }
            transaction.Commit();
            session.Close();
        }
        public void fillTable(DataGridView mainTable)
        {
            ISession session = ISessionManager.Instance.CreateISession();
            ICriteria query = session.CreateCriteria(typeof(TL1), "r")
            .SetFetchMode("Person", FetchMode.Join)
            .SetFetchMode("TL1", FetchMode.Join);
            IList<TL1> tl1List = query.List<TL1>();

            int step = 0;
            foreach (TL1 tl1 in tl1List)
            {
                mainTable.Rows.Add();
                mainTable.Rows[step].Cells[0].Value = tl1List[step].Id;
                mainTable.Rows[step].Cells[1].Value = tl1List[step].personTL1.AM;
                mainTable.Rows[step].Cells[2].Value = tl1List[step].personTL1.Name;
                mainTable.Rows[step].Cells[3].Value = tl1List[step].personTL1.SureName;
                mainTable.Rows[step].Cells[4].Value = tl1List[step].personTL1.Address;
                mainTable.Rows[step].Cells[5].Value = tl1List[step].personTL1.Phone;
                mainTable.Rows[step].Cells[6].Value = tl1List[step].ProjectGrade1;
                mainTable.Rows[step].Cells[7].Value = tl1List[step].ProjectGrade2;
                mainTable.Rows[step].Cells[8].Value = tl1List[step].ProjectGrade3;
                step++;
            }
        }
        public void clearTable(DataGridView mainTable)
        {
            mainTable.Rows.Clear();
        }
        public void signInCourse(int userId_)
        { 
        //nothning
        }
        #endregion
    }

    public class StudentBridge : IBridge
    {
        #region IBridge Members
        public void deleteUser(DataGridView mainTable)
        { 
        //nothing
        }
        public void fillTable(DataGridView mainTable)
        {
            ISession session = ISessionManager.Instance.CreateISession();
            ICriteria query = session.CreateCriteria(typeof(TL1), "r")
            .SetFetchMode("Person", FetchMode.Join)
            .SetFetchMode("TL1", FetchMode.Join);
            IList<TL1> tl1List = query.List<TL1>();


            int step = 0;
            //IList<Person> personList = personDao.GetAll();
            //foreach (Person person in personList)
            foreach (TL1 tl1 in tl1List)
            {
                mainTable.Rows.Add();
                mainTable.Rows[step].Cells[0].Value = tl1List[step].Id;
                mainTable.Rows[step].Cells[1].Value = tl1List[step].personTL1.AM;
                mainTable.Rows[step].Cells[2].Value = tl1List[step].ProjectGrade1;
                mainTable.Rows[step].Cells[3].Value = tl1List[step].ProjectGrade2;
                mainTable.Rows[step].Cells[4].Value = tl1List[step].ProjectGrade3;
                step++;
            }
        }
        public void clearTable(DataGridView mainTable)
        {
            mainTable.Rows.Clear();
        }
        public void signInCourse(int userId_)
        {
            IPersonDao personDao = DataAccessFactory.LoadPersonDao();
            ITransaction transaction = null;
            ISession session = ISessionManager.Instance.CreateISession();
            transaction = session.BeginTransaction();


            Person newPerson = new Person();
            newPerson = personDao.GetById(userId_);

            TL1 newTL1 = new TL1();
            newTL1.personTL1 = newPerson;

            session.Save(newTL1);
            transaction.Commit();
            session.Close();
        }
        #endregion
    }
    # endregion

    # region Abstraction

    public interface IAbstractBridge
    {
        void CallDeleteUser(DataGridView mainTable);
        void CallFillTable(DataGridView mainTable);
        void CallClearTable(DataGridView mainTable);
        void CallSignInCourse(int userId_);
    }

    public class AbstractBridge : IAbstractBridge
    {
        public IBridge bridge;
        public AbstractBridge(IBridge bridge)
        {
            this.bridge = bridge;
        }

        #region IAbstractBridge Members

        public void CallDeleteUser(DataGridView mainTable)
        {
            this.bridge.deleteUser(mainTable);
        }
        public void CallFillTable(DataGridView mainTable)
        {
            this.bridge.fillTable(mainTable);
        }
        public void CallClearTable(DataGridView mainTable)
        {
            this.bridge.clearTable(mainTable);
        }
        public void CallSignInCourse(int userId_)
        {
            this.bridge.signInCourse(userId_);
        }
        #endregion
    }
    # endregion
}
